package day_2022_4_to_7.day48;

import java.util.Stack;

/**
 * 括号匹配
 * @author haomin
 * @date 2022/04/25 11:04
 **/
public class Day7_2_Parenthesis {
    public static void main(String[] args) {
        System.out.println(chkParenthesis("())",3));
    }
    public static boolean chkParenthesis(String A, int n) {
        for (int i = 0; i < A.length(); i++) {
            if(!(A.charAt(i) == '(' || A.charAt(i) == ')')){
                System.out.println("输入错误");
                return false;
            }
        }
        Stack<Character> stack = new Stack<>();
        stack.push(A.charAt(0));
        int i = 1;
        while (i < n){
            if(A.charAt(i) == '('){
                stack.push(A.charAt(i));
            }else {
                if(stack.isEmpty()){
                    return false;
                }else {
                    stack.pop();
                }
            }
            i++;
        }
        return stack.isEmpty();
    }
}